package com.hhit.zhgd.controller

import cn.hutool.core.util.ObjectUtil
import com.hhit.zhgd.domain.entity.ZhgdDictionary
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdDictionaryService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.*
import javax.validation.Valid

@RestController
class ZhgdDictionaryController {
    @Autowired
    private val zhgdDictionaryService:ZhgdDictionaryService?=null


    /**
     * @description 添加数据字典
     * @author qianchengcheng
     * @date 2024/7/12 11:05
     */
    @PostMapping("addDictionary")
    fun addDictionary(@RequestBody zhgdDictionary: @Valid ZhgdDictionary?, bindingResult: BindingResult): BaseResp<Any?>? {
        if (bindingResult.hasErrors()) {
            val defaultMessage = bindingResult.fieldError?.defaultMessage
            return BaseResp.fail(defaultMessage)
        }
        return zhgdDictionaryService?.addDictionary(zhgdDictionary)
    }

    /**
     * @description 修改数据字典
     * @author qianchengcheng
     * @date 2024/7/12 11:14
     */
    @PostMapping("editDictionary")
    fun editDictionary(@RequestBody zhgdDictionary: @Valid ZhgdDictionary?, bindingResult: BindingResult): BaseResp<Any?>? {
        if (bindingResult.hasErrors()) {
            val defaultMessage = bindingResult.fieldError?.defaultMessage
            return BaseResp.fail(defaultMessage)
        }
        return zhgdDictionaryService!!.editDictionary(zhgdDictionary)
    }

    /**
     * @description 删除数据字典
     * @author qianchengcheng
     * @date 2024/7/12 11:18
     */
    @GetMapping("delDictionary")
    fun delDictionary(@RequestParam("id") id: Int?): BaseResp<Any?>? {
        return if (ObjectUtil.isNull(id)) {
            BaseResp.fail("无可删除的数据!")
        } else zhgdDictionaryService?.delDictionary(id)
    }

    /**
     * @description 分页查询
     * @author qianchengcheng
     * @date 2024/7/12 13:28
     */
    @GetMapping("getDictionaryPageInfo")
    fun getDictionaryPageInfo(
        @RequestParam(name = "keyWord") keyWord: String?,
        @RequestParam(name = "page", defaultValue = "1") page: Int,
        @RequestParam(name = "size", defaultValue = "15") size: Int
    ): BaseResp<Any?>? {
        return BaseResp.success(zhgdDictionaryService?.getDictionaryPageInfo(keyWord, page, size))
    }

    /**
     * @description 根据pCode获取数据
     * @author qianchengcheng
     * @date 2024/7/12 13:38
     */
    @GetMapping("getDictionaryByPCode")
    fun getDictionaryByPCode(@RequestParam("pCode") pCode: String?): BaseResp<Any?>? {
        return BaseResp.success(zhgdDictionaryService?.getDictionaryByPCode(pCode))
    }
}